package com.tangkun.environment.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.WuRanYuanAdapter;
import com.tangkun.environment.entity.gongzuo.EnterpriseReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.view.CustomListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 社区污染源 列表
 */

public class ShequWuRanYuanActivity extends BaseActivity implements CustomListView.OnLoadMoreListener, CustomListView.OnRefreshListener {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;

    public int page = 1;// 当前页
    private int totalCount = 0;// 总数量
    private boolean OnHeadRefreshTAG = true;// 为true时清楚已有数据

    /**
     * 数据源
     */
    private List<EnterpriseReturn.DataBean.ListBean> dataList = new ArrayList<EnterpriseReturn.DataBean.ListBean>();
    /**
     * 适配器
     */
    private WuRanYuanAdapter adapter;
    /**
     * names
     */
    private String names = "";
    /**
     * ID
     */
    private String id = "";
    /**
     * lat
     */
    private String lat = "";
    /**
     * lng
     */
    private String lng = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        id = getIntent().getStringExtra("id");
        lat = getIntent().getStringExtra("lat");
        lng = getIntent().getStringExtra("lng");
        names = getIntent().getStringExtra("names");
        setContentView(R.layout.activity_shequwuranyuan);
        ButterKnife.bind(this);
        if (!TextUtils.isEmpty(names)) {
            title.setText(names);
        } else {
            title.setText("");
        }
        initView();
        queryByOrgEnterprise();
    }

    private void initView() {
        customListView.setOnRefreshListener(this);
        customListView.setOnLoadListener(this);
        customListView.setCanLoadMore(true);
        customListView.setAutoLoadMore(true);
        adapter = new WuRanYuanAdapter(ShequWuRanYuanActivity.this, dataList);
        customListView.setAdapter(adapter);
    }

    @OnClick({R.id.back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
        }
    }


    @Override
    public void onRefresh() {
        page = 1;
        OnHeadRefreshTAG = true;
        customListView.setCanLoadMore(true);
        queryByOrgEnterprise();
    }

    @Override
    public void onLoadMore() {
        OnHeadRefreshTAG = false;
        if (page * pageSize < totalCount) {
            page++;
            queryByOrgEnterprise();
        } else {
            customListView.onLoadMoreCompleteNoData();
        }
    }

    /**
     * 污染源列表
     */
    private void queryByOrgEnterprise() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNumber", page + "");
        map.put("pageSize", pageSize + "");
        map.put("propertyid", id);
        map.put("lat", lat);
        map.put("lng", lng);
        fromNetwork.queryByOrgEnterprise(map).compose(new RxActivityHelper<EnterpriseReturn>().ioMain(ShequWuRanYuanActivity.this, true))
                .subscribe(new MySubscribe<EnterpriseReturn>(mContext, true) {
                    @Override
                    protected void success(EnterpriseReturn resultBean) {
                        if (resultBean.data != null) {
                            customListView.onLoadMoreComplete();
                            customListView.onRefreshComplete();
                            customListView.setCanLoadMore(true);
                            totalCount = resultBean.data.totalRow;
                            if (page * pageSize >= totalCount) {
                                customListView.setCanLoadMore(false);
                            }
                            //下拉刷新
                            if (OnHeadRefreshTAG) {
                                OnHeadRefreshTAG = false;
                                dataList.clear();
                            }
                            //填充数据
                            dataList.addAll(resultBean.data.list);
                            //刷新列表
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }
}
